<?php
namespace app\admin\controller;
use app\admin\controller\Base;
use think\Image;
use think\Controller;
use think\Db;
use think\Request;
class Bbs extends Base
{
    
    // 广告 - 列表 
    public function adList()
    {        
        
        $where=[];
        $where = ['admin_id'=>$this->accountId];
        $res = db('ad')
            ->where($where)
            ->field('id, display, title, path, thumb, sort')
            ->order('sort desc')
            ->paginate(20);
        $page = $res->render();
        $this->assign('res', $res);
        $this->assign('page', $page);
        return $this->fetch();
    }
    
    // 广告 - 修改
    public function adEdit(Request $request,$id)
    {
        $sk = $this->accountId;
        if (request()->isPost()) {
            $post = input('post.');
            //缩略图上传----开始----
            if (!empty($_FILES['thumb']['tmp_name'])) {
                // 获取表单上传文件 
                $file = request()->file('thumb');
                // 移动到框架应用根目录/public/uploads/ 目录下'
                $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
                // $info = $file->move('/home/www/upload/');
                if($info){
                    // 成功上传后 获取上传信息
                    // 输出 
                    // $img ='http://127.0.0.1/applet/'.'public' . DS . 'uploads' . '/' . $info->getSaveName();
                    $thumb = 'uploads' . '/' . $info->getSaveName();
                    $post['thumb'] = $thumb;
                }
            }
            //缩略图上传----结束----

            $where = [];
            $where['id'] = $post['id'];
            $where['admin_id'] = $this->accountId;
            $res = db('ad')->where($where)->update($post);
            if ($res) {
                    suc('','修改成功');
                    }else{
                    suc('','修改失败');
                    }
        }
        $res = db('ad')->where(['id'=>$id])->find();
        $this->assign('res', $res);
        return $this->fetch();
    }
    
    // 广告 - 增加
     public function adAdd(Request $request)
    {
        $sk = $this->accountId;
        if (request()->isPost()) {
            $post = request()->only(['title', 'path','thumb', 'sort'], 'post');
              //缩略图上传----开始----
              if (!empty($_FILES['thumb']['tmp_name'])) {
                // 获取表单上传文件 
                $file = request()->file('thumb');
                // 移动到框架应用根目录/public/uploads/ 目录下'
                $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
                // $info = $file->move('/home/www/upload/');
                if($info){
                    // 成功上传后 获取上传信息
                    // 输出 
                    // $img ='http://127.0.0.1/applet/'.'public' . DS . 'uploads' . '/' . $info->getSaveName();
                    $thumb = 'uploads' . '/' . $info->getSaveName();
                    $post['thumb'] = $thumb;
                }
            }else{
                 $post['thumb'] = '';
            } 
            //缩略图上传----结束----
            $post['admin_id'] = $this->accountId;
            return $this->BbsLogic->adAdd($post);
        }
        return $this->fetch();
    }

    
    // 广告 - 删除
    public function adDel($id)
    {
        $where = [];
        $where['id'] = $id;
        $where['admin_id'] = $this->accountId;
        $res = db('ad')->where($where)->delete();
        
        suc();
    }
    
    public function adAdds()
    {
        return json_encode($_REQUEST);
    }

    //分类 - 列表
    public function category()
    {
        $where=[];
        $where = ['admin_id'=>$this->accountId];
        $res = db('bbs_category')->where($where)->paginate(100, false);
        $page = $res->render();
        $res = getCategory($res, 0);
        $this->assign('res', $res);
        $this->assign('page', $page);
        return $this->fetch();
    }
    
    //分类 - 添加
    public function categoryAdd(Request $request)
    {
        $sk = $this->accountId;
        if (request()->isPost()) {
            $data = input('post.');
            /**图片开始**/
            $file = request()->file('thumb');
            if(!empty($file)){
                $path = 'public' . DS;
                $paths = 'uploads' . DS . $sk . DS . 'admin' . '/';
                // 移动到框架应用根目录/public/uploads/ 目录下'
                $info = $file->move(ROOT_PATH . $path . $paths);
                    
                // $info = $file->move('/home/www/upload/');
                if($info){
                    // 成功上传后 获取上传信息
                    $img = $paths . $info->getSaveName();
                    $post['thumb'] = $img;
                } else {
                    err('', $file->getError());
                }
            $data['thumb'] = $post['thumb'];
            }
            /**图片结束**/
            $data['admin_id'] = $this->accountId;
            $data['create_time'] = $this->timeStamp;
            $res = db('bbs_category')->insert($data);
            if ($res) {
                suc('','添加成功');
            }else{
                suc('','添加失败');
            }         
        }
        $where['admin_id'] = $this->accountId;
        $cate = db('bbs_category')->where($where)->limit(100)->select();
        $cate = getCategory($cate, 0);
        $this->assign('cate', $cate);
        return $this->fetch();
    }
    //分类 - 修改
    public function categoryEdit($id)
    {   

        if (request()->isPost()) {
            $data = input('post.');
            //图片上传
            if (!empty($_FILES['thumb']['tmp_name'])) {
                // 获取表单上传文件 
                $file = request()->file('thumb');
                // 移动到框架应用根目录/public/uploads/ 目录下'
                $info = $file->move(ROOT_PATH . 'public' . DS . 'uploads');
                // $info = $file->move('/home/www/upload/');
                if($info){
                    // 成功上传后 获取上传信息
                    // 输出 
                    // $img ='http://127.0.0.1/applet/'.'public' . DS . 'uploads' . '/' . $info->getSaveName();
                    $thumb = 'uploads' . '/' . $info->getSaveName();
                    $data['thumb'] = $thumb;
                }
            }

            if (empty($data['parent_id'])) {
                unset($data['parent_id']);
            }

            $where = [];
            $where['id'] = $id;
            $where['admin_id'] = $this->accountId;
            $res = db('bbs_category')->where($where)->update($data);
            if ($res) {
                suc('','修改成功');
            }else{
                suc('','修改失败1');
            } 
        }
            
        $where = ['admin_id'=>$this->accountId];
        $cate = db('bbs_category')->where($where)->limit(100)->select();
        foreach ($cate as $key=>$value) {
            if ($value['id'] == $id) {
                $res = $cate[$key];
            }
        }
        $cate = getCategory($cate, 0);
        $this->assign('cate', $cate);
            
        $this->assign('res', $res);
        return $this->fetch();


    }
    //分类 - 删除
    public function categoryDel($id)
    {
        $where = [];
        $where['id'] = $id;
        $where['admin_id'] = $this->accountId;
        $res = db('bbs_category')->where($where)->delete();
        
        suc();
    }


    //咨询管理 - 列表
    public function advisorylist()
    {

        $where=[];
        $where = ['admin_id'=>$this->accountId];
        $field = [];
        $res = db('bbs_comment')->where($where)->field($field)->order('id desc')->paginate($this->page, false);

        $page = $res->render();
        $this->assign('res', $res);
        $this->assign('page', $page);
        return $this->fetch();
    }
    //咨询管理-添加
    public function advisoryAdd()
    {
         if (request()->isPost()) {
            $post = request()->only(['id','admin_id', 'bbs_id', 'username','avatarurl','title','create_time'], 'post');
            $post['admin_id'] = $this->accountId;
            $post['create_time'] = time();
            $res = db('bbs_comment')->insert($post);
            suc();
        }
        
       
        return $this->fetch();
    }
    //咨询管理 - 删除
    public function advisoryDel($id)
    {
        $where = [];
        $where['id'] = $id;
        $where['admin_id'] = $this->accountId;
        $res = db('bbs_comment')->where($where)->delete();
        
        suc();
    }
     //咨询管理 - 修改
    public function advisoryEdit($id)
    {   

        if (request()->isPost()) {
            $post = request()->only([ 'id','bbs_id','username', 'avatarurl','title','admin_id','create_time','state'], 'post');

            $where = [];
            $where['id'] = $id;
            $where['admin_id'] = $this->accountId;
            $res = db('bbs_comment')->where($where)->update($post);
            suc();
        }
        $res = db('bbs_comment')->where(['id'=>$id])->find();
        $this->assign('res', $res);
        return $this->fetch();
    }
}